#################################
#################################
#################################
###
###
###  Replication file for, 
### ``Appropriators Not Position Takers:  The Distorting Effects of Electoral Incentives on Congressional Representation"
###
###
#################################
#################################


#######################
##Replication for Table 1.  
#######

####Loading the Model, Data, Words, and Mutual Information Words
load('FinalModel.RData')  ##the model fit to the press release data
load('SparseDocs.RData')  ##the press release data, as sparse matrix
load('WordsUsed.RData')   ##the words that are used 
load('FinalMutInf.RData') ##the mutual information between each word and each topic


##setting my length command
len<- length

##this identifies the stems for each topic
topics<- matrix(NA, nrow=44,ncol=10)
for(j in 1:44){
	topics[j,]<- words2[order(mut.inf[j,], decreasing=T)[1:10]]
	}
	
	
##ordering by topic usage, using soft classification to calculate. 

sums<- apply(output$rs, 2, sum)
ords<- order(sums, decreasing=T)
part1<- topics[ords, 1:5]


final<- cbind(part1, round(sums[ords]/sum(sums), 3))



#########################
########################



#####Replication for Figure 1
#####This figure is a little complicated, so a brief overview.  I first compute the scaling
#####Then, create the points and label them.  



#####first, creating the scaling

exp.pis<- output$gamma
for(j in 1:301){
	exp.pis[j,]<- exp.pis[j,]/sum(exp.pis[j,])
	}




dist.mat<- matrix(NA, nrow=301, ncol=301)
for(j in 1:301){
	for(k in 1:j){
		dist.mat[j,k]<- dist.mat[k,j]<- sum(abs(exp.pis[j,]-exp.pis[k,]))
		}
		}

set.seed(44553)
points<- cmdscale(dist.mat, k=2, eig=T)

##there is a rotation issue that might create problems for the replication of this figure (the horizontal axis is flipped). So
##I have saved a version of the scaling with the correct orientation.  Loading that now:
load('Scale.RData')



##classifying the senators
cats.people<- apply(output$phis, 1, which.max)
names<- c()
for(j in 1:3){
	names<- c(names, names(list.press[[j]]))
		}
names.people<- names





#########
###putting together the gray-scale
gray.1<- gray(0.1)
gray.2<- gray(0.2)
gray.3<- gray(0)
gray.4<- gray(0.4)
gray.5<- gray(0.5)

cols.grey<- rep(NA, 301)
cols.grey[which(cats.people==1)]<- gray.1
cols.grey[which(cats.people==2)]<- gray.2
cols.grey[which(cats.people==3)]<- gray.3
cols.grey[which(cats.people==4)]<- gray.4
cols.grey[which(cats.people==5)]<- gray.5




####setting up the points
pch.seq<- c(4, 15, 16, 17, 18)



plot(points$points[,1:2], pch='', xlab='', ylab='', axes=F, frame.plot=F, xlim=c(-0.625, 0.85))
for(j in 1:5){
		points(points$points[which(cats.people==j), 1:2], pch=pch.seq[j], col=cols.grey[which(cats.people==j)])	
	}

###putting the senator's names.  this is a little complicated to get position correct.  text(points$points, labels=names.people)
###will give all senators' locations.


names.exam<- c('Pryor2007', 'Dewine2006', 'Santorum2005', 'Chafee2005', 'Inhofe2006', 'Wyden2007', 'Kennedy2007', 'Cornyn2006', 'Whitehouse2007',  'Johnson2005', 'Tester2007', 'Salazar2007')
match.exam<- c()

for(j in 1:len(names.exam)){
match.exam[j]<- grep(names.exam[j], names.people)}

#points(points$points[match.exam, 1:2], pch=20, col='black', cex=2)

store1<- list()
store1$x<- c(-0.49, -0.55, -0.43)
store1$y<- c(-0.58, -0.02, 0.13)

store1a<- list()
store1a$x<- c(-0.49, -0.55, -0.43)
store1a$y<- c(-0.63, 0.03, 0.17)

cat4.sub<- match.exam[c(7, 8, 9)]

text(store1a, labels=c('Kennedy (D-MA), 2007','Cornyn (R-TX), 2005', 'Whitehouse (D-RI), 2007'), cex=1.25, col=gray.4)

for(z in 1:3){
	
		arrows(store1$x[z], store1$y[z], points$points[cat4.sub[z], 1], points$points[cat4.sub[z], 2], len=0.1, lwd=4, col=gray.4)
		}


store2<- list()
store2$x<- c(-0.2, -0.25)
store2$y<- c(-0.3, 0.35)

store2a<- list()
store2a$x<- c(-0.2, -0.25)
store2a$y<- c(-0.34, 0.37)

cat3.sub<- match.exam[c(5, 6)]


text(store2a, labels=c('Inhofe (R-OK), 2006', 'Wyden (D-OR), 2007'), cex=1.25, col=gray.3)
arrows(store2$x[1], store2$y[1], points$points[cat3.sub[1], 1], points$points[cat3.sub[1], 2], col=gray.3, lwd=4, len=0.1)
arrows(store2$x[2], store2$y[2], points$points[cat3.sub[2], 1], points$points[cat3.sub[2], 2], col=gray.3, lwd=4, len=0.1)





store3<- list()
store3$x<- c(0.33, 0.42, 0.06)
store3$y<- c(0.46, 0.27, 0.42)


store3a<- list()
store3a$x<- c(0.33, 0.42, 0.04)
store3a$y<- c(0.48, 0.3, 0.45)

cat5.sub<- match.exam[c(10, 11,12)]




text(store3a, labels=c('Johnson (D-SD), 2005', 'Tester (D-MT), 2007', 'Salazar (D-CO), 2007'), cex=1.25, col=gray.5)
arrows(store3$x[1], store3$y[1], points$points[cat5.sub[1], 1], points$points[cat5.sub[1], 2], col=gray.5, lwd=4, len=0.1)
arrows(store3$x[2], store3$y[2], points$points[cat5.sub[2], 1], points$points[cat5.sub[2], 2], col=gray.5, lwd=4, len=0.1)
arrows(store3$x[3], store3$y[3], points$points[cat5.sub[3], 1], points$points[cat5.sub[3], 2], col=gray.5, lwd=4, len=0.1)


store4<- list()
store4$x<- c(0.24, 0.48)
store4$y<- c(-0.5, -0.18)

store4a<- list()
store4a$x<- c(0.24, 0.57)
store4a$y<- c(-0.54, -0.2)


cat2.sub<- match.exam[c(3,4)]



text(store4a, labels=c('Santorum (R-PA), 2005', 'Chafee (R-RI), 2005'), cex=1.25, col=gray.2)
arrows(store4$x[1], store4$y[1], points$points[cat2.sub[1], 1], points$points[cat2.sub[1], 2], col=gray.2, lwd=4, len=0.1)
arrows(store4$x[2], store4$y[2], points$points[cat2.sub[2], 1], points$points[cat2.sub[2], 2], col=gray.2, lwd=4, len=0.1)




store5<- list()
store5$x<- c(0.41, 0.6)
store5$y<- c(-0.59, -0.64)

store5a<- list()
store5a$x<- c(0.41, 0.63)
store5a$y<- c(-0.62, -0.68)

cat1.sub<- match.exam[c(1,2)]



text(store5a, labels=c('Pryor (D-AR), 2007', 'DeWine (R-OH), 2006'), cex=1.25, col=gray.1)
arrows(store5$x[1], store5$y[1], points$points[cat1.sub[1], 1], points$points[cat1.sub[1], 2], col=gray.1, lwd=4, len=0.1)
arrows(store5$x[2], store5$y[2], points$points[cat1.sub[2], 1], points$points[cat1.sub[2], 2], col=gray.1, lwd=4, len=0.1)



par(cex.axis=1.4)
par(col.axis=gray(0.4))
axis(1, c(-0.7, -0.45, 0.7, 0.85), c('', 'National Policy Position Taking', 'Particularistic Credit Claiming', ''), tick=T, lwd.ticks=c(0, 0, 0, 1), col=gray(0.4))

axis(2, c(-0.71, -0.5, 0.3,  0.46), c('', 'Less Regional\n Policy Focus', 'More Regional\n Policy Focus', ''), lwd.ticks= 0 , col=gray(0.4), tick=T)

##final component, legend

	legend(x= 0.6, y= 0.50, c('Issue\nOriented', 'Domestic\nPolicy', 'Pork & Policy', 'Appropriators:\n Workers', 'Appropriators:\nFirefighters'), pch = c(17, 16, 18, 15, 4), col=c(gray.4, gray.3, gray.5, gray.2, gray.1), cex = 1.35, text.col=c(gray.4, gray.3, gray.5, gray.2, gray.1), title.col='black', bty='o', y.intersp=1.45, x.intersp=1, box.lwd=1, text.width=0.175)

################################
################################



#####Figure 2 replication
####loading aggregated measures of credit claiming and position taking
load('DepVars.RData')

exp.apps<- list.dep[[1]]
exp.subs<- list.dep[[2]]
diff<- exp.apps - exp.subs




par(mar=c(5, 4.5, 4, 3))
par(cex.axis = 1.5)
par(cex.lab = 1.5)
par(col.axis='black')

plot(diff,points$points[,1], col=cols.grey, pch='', frame.plot=F, xlim=c(min(diff), max(diff) + 0.05), xlab='Prop. Credit Claiming - Prop. Position Taking', ylab='', axes=F)
axis(1, seq(-0.5, 0.75, by=0.25), seq(-0.5, 0.75, by=0.25), col='black')



title(ylab='First Dimension, Expressed Priorities')
 axis(2, c(-0.7, -0.45, 0.675, 0.85), c('', 'National Position Taking', 'Particularistic Credit Claiming', ''), tick=T, lwd.ticks=c(0, 0, 0, 1), col=gray(0.4))



pch.people<- rep(NA, 301)
for(j in 1:5){
	pch.people[which(cats.people==j)]<- pch.seq[j]
	}
	



points(diff, points$points[,1], pch=pch.people, col=cols.grey, cex=1.5)

lines(lowess(points$points[,1]~diff), lwd=4)





legend(x= 0.55, y= 0.05, c('Issue\nOriented', 'Domestic\nPolicy', 'Pork & Policy', 'Appropriators:\n Workers', 'Appropriators:\nFirefighters'), pch = c(17, 16, 18, 15, 4), col=c(gray.4, gray.3, gray.5, gray.2, gray.1), cex = 1.5,text.col=c(gray.4, gray.3, gray.5, gray.2, gray.1), title.col='black', bty='o', y.intersp=1.1, x.intersp=1, box.lwd=1, text.width=0.175)




lwd<- 4
cex<- 1.65

col.use<- c(gray.1, gray.2, gray.3, gray.4, gray.5)


cornyn.1<- c(-0.35, -0.62)
arrows( cornyn.1[1], cornyn.1[2], diff[match.exam[8]], points$points[match.exam[8], 1] , len=0.1, col=col.use[4], lwd=lwd)
text(cornyn.1[1] + 0.125, cornyn.1[2], 'Cornyn (R-TX), 2005', cex=1.5, col=col.use[4])

kennedy.1<- c(-0.275, -0.50)
arrows(kennedy.1[1], kennedy.1[2], diff[match.exam[7]], points$points[match.exam[7], 1], len=0.1, col=col.use[4], lwd=lwd)
text(kennedy.1[1] + 0.15, kennedy.1[2], 'Kennedy (D-MA), 2007', cex=1.5, col=col.use[4])

whitehouse.1<- c(-0.48, -0.25)
arrows(whitehouse.1[1], whitehouse.1[2], diff[match.exam[9]], points$points[match.exam[9], 1], len=0.1, col=col.use[4], lwd=lwd)
text(whitehouse.1[1] + 0.03 , whitehouse.1[2]+ 0.035, 'Whitehouse (D-RI), 2007', cex=1.5, col=col.use[4])

wyden.1<- c(-0.35, 0.08) 
inhofe.1<- c(-0.14, -0.30)

arrows(wyden.1[1], wyden.1[2], diff[match.exam[6]], points$points[match.exam[6],1], len=0.1, lwd=lwd, col=col.use[3])
text(wyden.1[1],wyden.1[2] + 0.04, 'Wyden (D-OR), 2007', cex=1.5, col=col.use[3])


arrows(inhofe.1[1], inhofe.1[2], diff[match.exam[5]], points$points[match.exam[5],1], len=0.1, lwd=lwd, col=col.use[3])
text(inhofe.1[1] + 0.15,inhofe.1[2], 'Inhofe (R-OK), 2006', cex=1.5, col=col.use[3])

salazar.1<- c(-0.09, 0.3) 
arrows(salazar.1[1], salazar.1[2], diff[match.exam[12]], points$points[match.exam[12],1], len=0.1, lwd=lwd, col=col.use[5])
text(salazar.1[1] , salazar.1[2] + 0.04, 'Salazar (D-CO), 2007', cex=1.5, col=col.use[5])

johnson.1<- c(0.05, 0.52)
santorum.1<- c(0.17, 0.01)
arrows(johnson.1[1], johnson.1[2], diff[match.exam[10]], points$points[match.exam[10],1], len=0.1, lwd=lwd, col=col.use[5])
text(johnson.1[1], johnson.1[2] + 0.04, 'Johnson (D-SD), 2005', cex=1.5, col=col.use[5])

arrows(santorum.1[1], santorum.1[2], diff[match.exam[3]], points$points[match.exam[3],1], len=0.1, lwd=lwd, col=col.use[2])
text(santorum.1[1], santorum.1[2] - 0.04, 'Santorum (R-PA), 2005', cex=1.5, col=col.use[2])

tester.1<- c(0.23, 0.6)
chafee.1<- c(0.32, 0.098)

arrows(tester.1[1], tester.1[2], diff[match.exam[11]], points$points[match.exam[11],1], len=0.1, lwd=lwd, col=col.use[5])
text(tester.1[1], tester.1[2] + 0.04, 'Tester (D-MT), 2007', col=col.use[5], cex=1.5)

arrows(chafee.1[1], chafee.1[2], diff[match.exam[4]], points$points[match.exam[4],1], len=0.1, lwd=lwd, col=col.use[2])
text(chafee.1[1], chafee.1[2] - 0.04, 'Chafee (R-RI), 2005', col=col.use[2], cex=1.5)

pryor.1<- c(0.46, 0.27)
dewine.1<- c(0.49, 0.74)

arrows(pryor.1[1], pryor.1[2], diff[match.exam[1]], points$points[match.exam[1], 1], len=0.1, lwd=lwd, col=col.use[1])
text(pryor.1[1] + 0.04, pryor.1[2]-0.03, 'Pryor (D-AR), 2007', col=col.use[1], cex=1.5)

arrows(dewine.1[1], dewine.1[2], diff[match.exam[2]], points$points[match.exam[2], 1], len=0.1, lwd=lwd, col=col.use[1])
text(dewine.1[1], dewine.1[2]+ 0.04, 'DeWine (R-OH), 2006', col=col.use[1], cex=1.5)



###################
###################
#####Replication for Figure 3



dems<- c(1, 5, 6, 7,9, 10, 11, 13, 18, 19, 20, 23, 28, 30, 33, 
		36, 39, 40, 43, 44, 50, 54, 56, 57, 58, 59, 60, 62, 63, 
		64, 65, 66, 67, 73, 75, 76, 77, 78, 79,81,82, 84, 85, 91, 100,
		101, 105, 106, 107, 109, 110, 111, 113,118, 119, 120, 123,128,
		132, 135, 138, 139, 142, 143, 149, 153, 155, 156, 157, 158, 159,
		161, 162, 163, 164, 165, 166, 172, 173, 175, 176, 177, 178, 179,
		181, 182, 184, 185, 191,200, 201, 205, 206, 207, 209, 210, 
		211, 213, 214, 218, 219, 220, 221, 222, 224, 229, 235, 
		238, 239, 242, 243, 248, 252, 254, 255, 256, 257, 258, 
		260, 261, 262, 263, 264, 265,270, 272, 273, 275, 276, 277, 278, 
		279, 281, 282, 283, 284, 290, 293, 299, 300, 301 )
party<- rep(0,301)
party[dems]<- 1


load('PresParty.RData')




par(cex.axis=1.5)
par(cex.lab=1.5)	
par(cex.main=1.5)
col.rep<- gray(0.5)
col.dem<- gray(0.1)


col.rep2<- gray(0.7)
col.dem2<- gray(0.3)

col.use<- gray(0.7)



plot(diff~party.rat, pch=20,, ylim=c(-0.5, 0.52), xlab='Two Party Vote Share, Bush', ylab='Prop. Credit - Prop.Position', frame.plot=T, col=col.use, cex=1)


##putting together the points for the senators on this plot
points.test<- cbind(diff[match.exam], party.rat[match.exam])
points.test[7,2]<- points.test[7,2] + 0.015

text(points.test[,1]~points.test[,2], labels=c('Pryor\n(D-AR), 2007', 'DeWine\n(R-OH),2006', 'Santorum\n(R-PA), 2005', 'Chafee\n (R-RI), 2005', 'Inhofe\n(R-OK), 2006', 'Wyden\n(D-OR),2007', 'Kennedy\n(D-MA), 2007','Cornyn\n(R-TX), 2006', 'Whitehouse\n(D-RI), 2007', 'Johnson\n(D-SD), 2005', 'Tester\n(D-MT), 2007', 'Salazar\n(D-CO),2007'), cex=1.25, col=c(col.dem, col.rep, col.rep, col.rep, col.rep, col.dem, col.dem, col.rep, col.dem, col.dem, col.dem, col.dem))

lines(lowess(diff[which(party==1)]~party.rat[which(party==1)], iter=0, f =3/5), col=col.dem, lwd=3)
lines(lowess(diff[which(party==0)]~party.rat[which(party==0)], iter=0, f = 3/5), col=col.rep, lwd=3)

text(0.4, 0.07, labels='Republicans', col=col.rep, cex=1.75)
text(0.7, -0.01, labels='Democrats', col=col.dem, cex=1.75)
axis(3, c(0.4, 0.5, 0.6, 0.7), c('', '', '', ''))
axis(4, c(-0.4, -0.2, 0, 0.2, 0.4), c('', '', '', '', ''))



###################
###################
###################
###################
##Replication for Table 1. 

num.press<- c()
a<- 0 
for(j in 1:3){
	dd<- list.press[[j]]
	for(k in 1:len(dd)){
		a<- a + 1
		num.press[a]<- len(dd[[k]])
		}
		}	
load('Variables.RData')	

centered.rat<- variables[[1]]
party<- variables[[2]]
majority<- variables[[3]]
on.cycle<- variables[[4]]
house<- variables[[5]]
freshman<- variables[[6]]
state.pop<- variables[[7]]
ind.indic<- variables[[8]]
state.indic<- variables[[9]]
ideal.vec<- variables[[10]]
approp.mem<- variables[[11]]
years<- variables[[12]]

library(lme4)

##columns 1-4
column1<- lmer(diff~centered.rat + party + majority + on.cycle +  I(years/100)  + house + freshman +  I(state.pop/1e7) + (1|ind.indic)  + (1|state.indic))

column2<- lmer(diff~centered.rat + party + majority + on.cycle +  I(years/100)  + house +  freshman +  I(state.pop/1e7) + I(ideal.vec) + I(ideal.vec^2) +  (1|ind.indic)  + (1|state.indic))

column3<- lmer(diff~centered.rat + party + majority + on.cycle +  I(years/100)  + house +  freshman +  I(state.pop/1e7) + approp.mem +  (1|ind.indic)  + (1|state.indic))

column4<- lmer(exp.apps~centered.rat + party + majority + on.cycle +  I(years/100)  + house +  freshman +  I(state.pop/1e7) +  (1|ind.indic)  + (1|state.indic))

column5<- lmer(exp.subs~centered.rat + party + majority + on.cycle +  I(years/100)  + house +  freshman +  I(state.pop/1e7) +  (1|ind.indic)  + (1|state.indic))




num.approp<- round(exp.apps*num.press)
num.sub<- round(exp.subs*num.press)

diff.count<- num.approp - num.sub

column6<- lmer(diff.count~centered.rat + party + majority + on.cycle +  I(years/100)  + house +  freshman +  I(state.pop/1e7) +  (1|ind.indic)  + (1|state.indic))

column7<- glmer(num.approp~centered.rat + party + majority + on.cycle +  I(years/100)  + house +  freshman +  I(state.pop/1e7) +  (1|ind.indic)  + (1|state.indic), family=poisson)


column8<- glmer(num.sub~centered.rat + party + majority + on.cycle +  I(years/100)  + house +  freshman +  I(state.pop/1e7) +  (1|ind.indic)  + (1|state.indic), family=poisson)




###################
###################
###################
###################
##Replication for Figure 4

load('ListFig4.RData')

scale.301<- list.fig4[[1]]
intell.301<- list.fig4[[2]]
iraq.301<- list.fig4[[3]]
immg<- list.fig4[[4]]
pat<- list.fig4[[5]]
iraq<- list.fig4[[6]]

###Immigration Figure


reg<- lmer(exp.pis[,22]~scale.301*immg + (1|ind.indic) + (1|state.indic))

reg.samp<- mcmcsamp(reg, 1000)
reg.coef<- t(reg.samp@fixef)

quant.store.immg<- matrix(NA, nrow=100, ncol=5)

seq.ideal<- seq(min(scale.301), max(scale.301), len=100)

for(j in 1:100){
	test1<- c(1, seq.ideal[j], 0.7, seq.ideal[j]*0.7); test2<- c(1, seq.ideal[j], 0.6, seq.ideal[j]*0.6)
	diff1<- reg.coef%*%test1 - reg.coef%*%test2
	quant.store.immg[j,]<- quantile(diff1, c(0.025, 0.1, 0.5, 0.9, 0.975))
	}
	

plot(quant.store.immg[,3]~seq.ideal, ylim=c(-0.075, 0.075), pch=20, cex=0.5, type='l', lwd=3, xlab='Immigration Scaling', ylab='Change in Attention to Immigration', frame.plot=T)	

part1<- c(quant.store.immg[,1], rev(quant.store.immg[,5]))
part2<- c(seq.ideal, rev(seq.ideal))

polygon(part1~part2, col=gray(0.8),  border=00000)
lines(quant.store.immg[,3]~seq.ideal, lwd=3)


abline(h=0, lty=2)

title(main='Attention to Immigration')

axis(3, c(-2, -1, 0, 1, 2), c('', '', '', '', ''))
axis(4, c(-0.05, 0, 0.05), c('', '', ''))
	



##Intelligence Figure


intell.vote<- lmer(exp.pis[,41]~pat*intell.301 + (1|ind.indic) + (1|state.indic))

intell.samp<- mcmcsamp(intell.vote, 1000)
intell.coef<- t(intell.samp@fixef)

quant.store<- matrix(NA, nrow=100, ncol=5)

seq.ideal<- seq(min(intell.301), max(intell.301), len=100)

for(j in 1:100){
	test1<- c(1, 0.7, seq.ideal[j], seq.ideal[j]*0.7); test2<- c(1, 0.6, seq.ideal[j], seq.ideal[j]*0.6)
	diff1<- intell.coef%*%test1 - intell.coef%*%test2
	quant.store[j,]<- quantile(diff1, c(0.025, 0.1, 0.5, 0.9, 0.975))
	}


	
par(cex.axis=1.5)
par(cex.lab=1.5)	
par(cex.main=1.5)	
plot(quant.store[,3]~seq.ideal, ylim=c(-0.075, 0.075), pch=20, cex=0.5, type='l', lwd=3, xlab='Intelligence Scaling', ylab='Change in Attention to Intelligence', frame.plot=T, axes=F)

part1<- c(quant.store[,1], rev(quant.store[,5]))
part2<- c(seq.ideal, rev(seq.ideal))
polygon(part1~part2, col=gray(0.8),  border=00000)
	
lines(quant.store[,3]~seq.ideal, lwd=3)
abline(h=0, lty=2)

axis(1, c(-2, -1, 0, 1, 2))
axis(3, c(-2, -1, 0, 1, 2), c('', '', '', '', ''))
axis(2, c(-0.05, 0, 0.05))
axis(4, c(-0.05, 0, 0.05), c('', '', ''))

title(main='Attention to Intelligence')


###Iraq Figure

iraq.reg<- lmer(exp.pis[,37]~iraq.301*iraq+ (1|ind.indic) + (1|state.indic))
iraq.samp<- mcmcsamp(iraq.reg, 1000)
iraq.coef<- t(iraq.samp@fixef)

quant.store<- matrix(NA, nrow=100, ncol=5)

seq.ideal<- seq(min(iraq.301), max(iraq.301), len=100)

for(j in 1:100){
	test1<- c(1,  seq.ideal[j],0.7,  seq.ideal[j]*0.7); test2<- c(1, seq.ideal[j], 0.6, seq.ideal[j]*0.6)
	diff1<- iraq.coef%*%test1 - iraq.coef%*%test2
	quant.store[j,]<- quantile(diff1, c(0.025, 0.1, 0.5, 0.9, 0.975))
	}

par(cex.axis=1.5)
par(cex.lab=1.5)	
par(cex.main=1.5)
plot(quant.store[,3]~seq.ideal, ylim=c(-0.075, 0.075), pch=20, cex=0.5, type='l', lwd=3, xlab='Iraq Scaling', ylab='Change in Attention to Iraq', frame.plot=T)	


part1<- c(quant.store[,1], rev(quant.store[,5]))
part2<- c(seq.ideal, rev(seq.ideal))

polygon(part1~part2, border=00000, col=gray(0.8))
lines(quant.store[,3]~seq.ideal, lwd=3)


abline(h=0, lty=2)
axis(3, c(-2, -1, 0, 1, 2), c('', '', '', '', ''))
axis(4, c(-0.05, 0, 0.05), c('', '', ''))


title(main='Attention to Iraq')

 


#################
################
##Figure 5 replication

load('DataFinal.RData')



func.obj<- function(temp, preds, party){

mat.calc<- matrix(NA, nrow=len(preds), ncol=2)
for(j in 1:len(preds)){
	mat.calc[j,]<- c(temp$fit[j]-1.28125*temp$se.fit[j], temp$fit[j] + 1.28125*temp$se.fit[j])
	}
	

part1<- c(mat.calc[,1], rev(mat.calc[,2]))
part2<- c(preds, rev(preds))

col<- gray(0.75)
	
polygon(part2, part1, col=col[1], border=NA)
lines(temp$fit~preds, col='black', lwd=3)

}


##creating the bootstrap draws
boot.strap<- function(h = 1/5){

	data.clean<- data.final
	samps<- sample(1:nrow(data.clean), nrow(data.clean), replace=T)
	data.new<- data.clean[samps,]
	less.than<- which(data.new$Party==1)
	selected<- data.new[less.than,]
	temp<- 1
	line1<- lowess(selected[,temp]~selected$ideal.final, f = h, iter =0 )
	greater.than<- which(data.new$Party==0)
	selected<- data.new[greater.than,]
	line2<- lowess(selected[,temp]~selected$ideal.final, f = h, iter = 0 )
	output<- list(line1, line2)
	names(output)<- c('lower', 'upper')
	return(output)
	}

list.boot<- list()
for(j in 1:1000){
	list.boot[[j]]<- boot.strap(h=2/3)
	}


ranges.dems<- quantile(data.final$ideal.final[which(data.final$Party==1)], seq(0, 1, by=0.075))
ranges.reps<- 	quantile(data.final$ideal.final[which(data.final$Party==0)], seq(0, 1, by=0.075))
	
func.average<- function(bot, top, party){
	sub<- which(data.final$Party==party)
	sub<- data.final[sub,]
	sel<- which(sub$ideal.final>bot & sub$ideal.final<top)
	resp<- mean(sub[sel,1])
	id<- mean(sub[sel,]$ideal.final)
	return(c(resp, id))
	}
	
mat.dem<-mat.rep<- matrix(NA, nrow=	len(ranges.dems)-1, ncol=2)
for(j in 1:(len(ranges.dems)-1)){
	mat.dem[j,]<- func.average(ranges.dems[j], ranges.dems[j+1], 1)
	mat.rep[j,]<- func.average(ranges.reps[j], ranges.reps[j+1], 0)
	}



##this puts together the left-hand plot 
plot(data.final[,1]~data.final$ideal.final, pch='', xlab='Ideal Point Estimate', ylim=c(0, 0.6), ylab='Prob, Iraq Press Release', frame.plot=F, col=gray(0.2))


out<- loess(data.final[which(data.final$Party==1),1]~data.final$ideal.final[which(data.final$Party==1)])

un.ideal<- unique(data.final$ideal.final[which(data.final$Party==1)])
un.ideal<- un.ideal[order(un.ideal)]	
temp<- predict(out, un.ideal, se=T)

func.obj(temp, un.ideal, party=1)

points(mat.dem[,1]~mat.dem[,2], pch=20, col=gray(0.2))


##same for GOP
out<- loess(data.final[which(data.final$Party==0),1]~data.final$ideal.final[which(data.final$Party==0)])

un.ideal<- unique(data.final$ideal.final[which(data.final$Party==0)])
un.ideal<- un.ideal[order(un.ideal)]	
temp<- predict(out, un.ideal, se=T)

##calculating se's

func.obj(temp, un.ideal, party=0)
points(mat.rep[,1]~mat.rep[,2], pch=20, col=gray(0.2))


dem.loc<- c(-1.25, 0.5)
rep.loc<- c(1, 0.14)

text(dem.loc[1], dem.loc[2], labels='Democrats', cex=1.5)
text(rep.loc[1], rep.loc[2], labels='Republicans', cex=1.5)




###########################################
#####putting together the right hand plot
mean.d.n<- mean.r.n<-mean.d.t<- mean.r.t<- mean.t<- mean.n<- c()
d1<- mean(data.final$ideal.final[which(party==1)])
r1<- mean(data.final$ideal.final[which(party==0)])
dist<- ifelse(data.final$Party==1, d1 - data.final$ideal.final, r1 - data.final$ideal.final)

for(j in 1:18){
	sub<- which(data.final$episode==j)
	dd<- data.final[sub,]
	cc<- dist[sub]
	mean.t[j]<- mean(cc[which(dd$Iraq==1)])
	mean.n[j]<- mean(cc[which(dd$Iraq==0)])
	mean.d.t[j]<- mean(dd$ideal.final[which(dd$Iraq==1 & dd$Party==1)] )
	mean.r.t[j]<- mean(dd$ideal.final[which(dd$Iraq==1 & dd$Party==0)])
	mean.d.n[j]<- mean(dd$ideal.final[which(dd$Iraq==0 & dd$Party==1)] )
	mean.r.n[j]<- mean(dd$ideal.final[which(dd$Iraq==0 & dd$Party==0)])
	
	}
	
##calculating for each of the 18 weeks
mean.n<- mean.t<- ep.mean.r<- ep.mean.d<- c()
for(j in 1:18){
	sub<- which(data.final$episode==j)
	dd<- data.final[sub,]
	ep.mean.d[j]<- mean(dd$ideal.final[which(dd$Party==1)])
	ep.mean.r[j]<- mean(dd$ideal.final[which(dd$Party==0)])
	dist.ep<- ifelse(dd$Party==1, ep.mean.d[j] - dd$ideal.final, dd$ideal.final - ep.mean.r[j])
	mean.t[j]<- mean(dist.ep[which(dd$Iraq==1)])
	mean.n[j]<- mean(dist.ep[which(dd$Iraq==0)])
	}
	

##creating the balance plot

one.balance<- function(point1, point2){
	plot(c(0,1)~c(0,1), pch='', xlim=c(-0.3, 0.55), ylim=c(1,19.5), xlab='Extremity', frame.plot=F, ylab='', axes=F)
	axis(1, c(-0.25, 0, 0.25, 0.5))
	#axis(2, seq(1, 18, by=1))
	for(j in 1:18){
		arrows(-0.3, j, 0.55, j, len=0, lwd=1, lty=2, col=gray			(0.7))
		arrows(0, 0, 0, 18, len=0, col=gray(0.7), lty=2)
		text(-0.3, j, as.character(j), cex=1.4)
		points(point1[j], j, pch=20, cex=2)
		points(point2[j], j, cex=2)
		}
	arrows(point2[18]-0.025, 18.5,point2[18], 18.2, len=0.1) 
	text(point2[18]-0.025, 19.25, 'Mean Extremity\n No Iraq PR')
	arrows(point1[18]+0.025, 18.5, point1[18], 18.2, len=0.1)
	text(point1[18]+0.025, 19.25, 'Mean Extremity\n Iraq PR')
	}
par(cex.axis=1.5)
par(cex.lab=1.5)	
	
one.balance(mean.t, mean.n)



############Figure 6
############


##identifying the topics
substantive<- c(37, 26, 24, 14, 40, 22, 10, 41, 34, 36, 33, 43, 15, 16,25, 8, 5, 42)
approp.cat<-  c(3, 7 , 13, 21, 23,30, 31, 38)



##this calculates the relative extremity for the legislators


load('CleanNames.RData')
press.data<- read.delim("PressData.csv", sep=',')





each.issue<- c()
for(j in 1:44){
	temp<- which(press.data$topic==j)
	each.issue[j]<- mean(press.data$extr[temp])
	}

subs<- rep(0,44)
subs[substantive]<- 1
cols<- rep(gray(0.35), 44)
cols[substantive]<- gray(0)
cols[approp.cat]<-  gray(0.7)


labels<- c('Honor', 'Trans. Grants', 'Iraq', 'DHS Policy', 'Heritage', 'Judicial Nom.', 'Fire Grant', 'WRDA', 
			'Edu. Fund', 'Budget', 'Cons. Safety', 'Health Access', 'Research', 'Just. Grants', 'Environ.',
			'Biofuel','Immigration', 'Farm', 'Def. Const.', 'Energy/Gas', 'Just. Dept.', 'Def Budg.', 'Work. Rights', 
			'Mort. Crisis', 'Vet. Affairs', 'BRAC', 'Imports', 'Gov. Transp', 'For. Affairs', 'Education', 'Tax Pol.', 
			'Transport', 'Med_LIHEAP', 'Disasters', 'Child Safety', 'SCHIP', 'Prev. Med.','Stem Cells', 'Katrina', 'Inf. Disease', 'FDA', 'Soc. Sec', 
			'Jus. Oversight', 'Work. Safety')


order.docs<- order(apply(output$rs, 2, sum), decreasing=T)
realign<- c()
for(j in 1:44){
	realign[order.docs[j]]<- labels[j]
	}


par(mar=c(4, 2, 1, 1))
plot(c(0,1)~c(0,1), pch='', axes=F, xlab='Average Extremity', ylab='', frame.plot=F, xlim=c(-0.225, 0.2), ylim=c(1, 44))
axis(1, c(-0.15, -0.1, -0.05, 0,0.05, 0.1,0.15, 0.2))
temp<- order(each.issue, decreasing=F)
arrows(0,1, 0, 44, len=0, lty=2, col=gray(0.6), lwd=3)
pchs<- rep(1, 44)
pchs[c(substantive, approp.cat)]<- 20
cexs<- rep(3, 44)





for(j in len(temp):1){
	arrows(-0.15, j, 0.2, j, len=0, lty=2, col=gray(0.6))
	text(-0.2, j, realign[temp[j]], col=cols[temp[j]])
	points(each.issue[temp[j]], j, col=cols[temp[j]], pch=pchs[temp[j]], cex=cexs[temp[j]])
	}
	
##note that the open circles could easily be grouped with particularistic spending.  And as I show in the Supplementary Information, they strengthen the findings.  	

